package com.example.zpositionanimationdemo.slice;

import com.example.zpositionanimationdemo.ResourceTable;
import com.example.zpositionanimationdemo.chechbox.RCheckBox;
import com.sxu.shadowdrawable.ShadowDrawable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AbsButton;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.WindowManager;

public class ZPositionAnimationSlice extends AbilitySlice {
    private Button zpo_but;
    private RCheckBox optOutCheckbox;
    boolean isCheck_but = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_zpositionanimation);
        initView();
        initDate();
        onclick();
    }

    private void onclick() {
        zpo_but.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (isCheck_but == false){
                    ShadowDrawable.setShadowDrawable(zpo_but, Color.getIntColor("#FFFFFF"), dpToPx(0),
                            Color.getIntColor("#FFCACACA"), dpToPx(4), 0, 4);
                }else {
                    ShadowDrawable.setShadowDrawable(zpo_but, Color.getIntColor("#FFFFFF"), dpToPx(0),
                            Color.getIntColor("#FFCACACA"), dpToPx(6), 0, 6);
                }

                getContext().getUITaskDispatcher().delayDispatch(new Runnable() {
                    @Override
                    public void run() {
                        ShadowDrawable.setShadowDrawable(zpo_but, Color.getIntColor("#FFFFFF"), dpToPx(0),
                                Color.getIntColor("#FFFFFFFF"), dpToPx(0), 0, 0);
                    }
                },50);
            }
        });

        optOutCheckbox.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean isBoolean) {
                absButton.setChecked(isBoolean);
                if (isCheck_but == true){
                    isCheck_but = false;
                }else {
                    isCheck_but = true;
                }
            }
        });
    }

    private int dpToPx(int dp) {
        double density = DisplayManager.getInstance().getDefaultDisplay(this).get().getAttributes().densityPixels;
        return (int) ( dp * density + 0.5f);
    }

    private void initDate() {
    }

    private void initView() {
        optOutCheckbox = (RCheckBox) findComponentById(ResourceTable.Id_optOutCheckbox);
        zpo_but = (Button) findComponentById(ResourceTable.Id_zpo_but);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(-13615201);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
        isCheck_but = false;
        optOutCheckbox.setChecked(false);
    }
}
